<template>
	<div class="system-user-container layout-padding">
		<el-card shadow="hover" class="layout-padding-auto">
			<div class="system-user-search mb15">
				<el-input v-model="state.tableData.param.param.user_name.val" size="default" placeholder="请输入账号" style="max-width: 180px;"> </el-input>
				<el-input v-model="state.tableData.param.param.nickname.val" size="default" placeholder="请输入昵称" style="max-width: 180px;"> </el-input>
				<el-select v-model="state.tableData.param.param.auth_id.val" placeholder="请选择角色" clearable size="default" style="max-width: 180px;">
					<el-option v-for="(item,index) in state.roleData" :key="item.id" :label="item.role_name" :value="item.id"></el-option>
				</el-select>
				
				<el-button size="default" type="primary" @click="seachClick">
					<el-icon>
						<ele-Search />
					</el-icon>
					查询
				</el-button>
				<el-button size="default" type="success" @click="onOpenAddUser('add')" v-auth="'/auth/admin/add'">
					<el-icon>
						<ele-FolderAdd />
					</el-icon>
					新增管理员
				</el-button>
			</div>
			<el-table :data="state.tableData.data" v-loading="state.tableData.loading" style="width: 100%">
				<el-table-column prop="id" label="ID" show-overflow-tooltip width="80px"></el-table-column>
				<el-table-column prop="user_img" label="头像">
					<template #default="scope">
						<el-avatar :src="state.imgUrl + scope.row.user_img">
							<img :src="chatHealthTou" />
						</el-avatar>
					</template>
				</el-table-column>
				<el-table-column prop="user_name" label="账号" show-overflow-tooltip></el-table-column>
				<el-table-column prop="nickname" label="昵称" show-overflow-tooltip></el-table-column>
				<el-table-column prop="role.role_name" label="关联角色" show-overflow-tooltip></el-table-column>
				<el-table-column prop="status" label="用户状态" show-overflow-tooltip>
					<template #default="scope">
						<el-tag type="success" v-if="scope.row.status == 1">启用</el-tag>
						<el-tag type="info" v-else>禁用</el-tag>
					</template>
				</el-table-column>
				<el-table-column prop="last_login_ip" label="最后登录ip" show-overflow-tooltip></el-table-column>
				<el-table-column label="最后登录时间" show-overflow-tooltip>
					<template #default="scope">
						<span>{{ dateFormatYMDHMS(scope.row.last_login_time * 1000)}}</span>
                    </template>
				</el-table-column>
				<el-table-column label="创建时间" show-overflow-tooltip>
					<template #default="scope">
						<span>{{ dateFormatYMDHMS(scope.row.last_login_time * 1000)}}</span>
                    </template>
				</el-table-column>
				<el-table-column label="操作" width="100">
					<template #default="scope">
						<el-button size="small" text type="primary" @click="onOpenEditUser('edit', scope.row)" v-auth="'/auth/admin/edit'">修改</el-button>
						<el-button size="small" text type="primary" @click="onRowDel(scope.row)" v-auth="'/auth/admin/del'">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<el-pagination
				@size-change="onHandleSizeChange"
				@current-change="onHandleCurrentChange"
				class="mt15"
				:pager-count="5"
				:page-sizes="[10, 20, 30]"
				v-model:current-page="state.tableData.param.page"
				background
				v-model:page-size="state.tableData.param.limit"
				layout="total, sizes, prev, pager, next, jumper"
				:total="state.tableData.total"
			>
			</el-pagination>
		</el-card>
		<UserDialog ref="userDialogRef" @refresh="getTableData()" />
	</div>
</template>

<script setup name="admin">
import { defineAsyncComponent, reactive, onMounted, ref,nextTick } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import { adminApi } from '/@/api/admin/index';
import { roleApi } from '/@/api/role/index';
// 引入组件
const UserDialog = defineAsyncComponent(() => import('./dialog.vue'));
import commonFunction from '/@/utils/commonFunction';
// 定义变量内容
const { dateFormatYMDHMS } = commonFunction();
import chatHealthTou from '/@/assets/error_tou.png'
import { set } from 'nprogress';
// 定义变量内容
const userDialogRef = ref();

const paramOp = {
	nickname:'%%'
}
const state = reactive({
	roleData:[],
	imgUrl:import.meta.env.VITE_IMG_URL,
	tableData: {
		data: [],
		total: 0,
		loading: false,
		param: {
			param:{
				user_name:{},
				nickname:{},
				auth_id:{}
			},
			page: 1,
			limit: 10,
		},
	},
});

const seachClick = () =>{
	onHandleCurrentChange(1)
}
// 初始化表格数据
const getTableData = () => {
	state.tableData.loading = true;
	for (let key in state.tableData.param.param) {
		if (state.tableData.param.param[key].val && state.tableData.param.param[key].val !== '') {
			state.tableData.param.param[key].op = paramOp[key] || '='
		}else{
			state.tableData.param.param[key] = {}
		}
	}
	adminApi().index(state.tableData.param).then(res=>{
        state.tableData.data = res.data.list;
        state.tableData.total = res.data.total;
        state.tableData.loading = false;
    })
};
// 打开新增用户弹窗
const onOpenAddUser = (type) => {
	userDialogRef.value.openDialog(type);
};
// 打开修改用户弹窗
const onOpenEditUser = (type, row) => {
	userDialogRef.value.openDialog(type, row);
};
// 删除用户
const onRowDel = (row) => {
	ElMessageBox.confirm(`此操作将永久删除账户：“${row.user_name}”，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
	})
		.then(() => {
			adminApi().del({ids:row.id}).then(res=>{
                getTableData();
                ElMessage.success('删除成功');
            })
		})
		.catch(() => {});
};
// 分页改变
const onHandleSizeChange = (val) => {
	state.tableData.param.limit = val;
	getTableData();
};
// 分页改变
const onHandleCurrentChange = (val) => {
	state.tableData.param.page = val;
	getTableData();
};
const getRoleData = () => {
	roleApi().index().then(res=>{
        state.roleData = res.data.list;
		setTimeout(()=>{
			userDialogRef.value.state.roleData = state.roleData;
		},100)
    })
}
// 页面加载时
onMounted(() => {
	getTableData();
	getRoleData()
});

</script>

<style scoped lang="scss">
.system-user-container {
	:deep(.el-card__body) {
		display: flex;
		flex-direction: column;
		flex: 1;
		overflow: auto;
		.el-table {
			flex: 1;
		}
	}
}
</style>
